/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2016-03-26 21:00:24
 * Last Modified: 2017-04-24 16:48:55
 * File Name: controllers/userinfo.go
 * Description:
 */
package controllers

import (
	"encoding/json"
	"time"

	"qkleague/account"
	"qkleague/errorcode"

	log "github.com/cihub/seelog"
	//rpc "qkleague/rpc/client/openapi"
)

type UserInfoController struct {
	LoginedController
}

type userinfoItem struct {
	//account.UserInfo
	account.QKLeague
	Area       string `json:"area"`
	Active     int    `json:"active"`
	CreateTime string `json:"create_time"`
	UpdateTime string `json:"update_time"`
}

// @router /userprofile/query [get]
//身份证信息，密码等敏感信息不返回
func (p *UserInfoController) GetUserInfo() {
	defer p.Output("GetUserInfo")
	userid := p.GetString("uid", "")
	ui := account.GetAccountService().GetUserInfoByUserid(userid)
	if ui == nil {
		p.Resp["code"] = errorcode.EC_QKLEAGUE_USER_NOT_EXIST
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_QKLEAGUE_USER_NOT_EXIST)
		return
	}
	user := &userinfoItem{}
	//user.UserId = ui.UserId
	user.LeagueId = ui.LeagueId
	user.NickName = ui.NickName
	//user.Avatar = ui.Avatar
	user.Mobile = ui.Mobile
	//user.Birthday = ui.Birthday
	//user.Slogan = ui.Slogan
	//user.RegionCode = ui.RegionCode
	user.RegionAddr = ui.RegionAddr
	//user.Area = rpc.ParseRegionCode(user.RegionCode)["admin_addr"].(string)
	user.CreateTime = time.Unix(ui.InsertTick, 0).Format("2006-01-02 03:04:05")
	user.UpdateTime = time.Unix(ui.UpdateTick, 0).Format("2006-01-02 03:04:05")
	user.Active = 1
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	p.Resp["user"] = user
}

// @router /userprofile/update [post]
//修改除了密码之外的账号信息
func (p *UserInfoController) UpdateUserInfo() {
	defer p.Output("UpdateUserInfo")
	body := p.Ctx.Input.RequestBody
	log.Infof("body: %s", string(body))
	data := make(map[string]interface{})
	if err := json.Unmarshal(body, &data); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	userId := p.GetString("uid", "")
	if userId == "" {
		p.Resp["code"] = errorcode.EC_USER_INFO_USER_ID_MISSING
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_USER_INFO_USER_ID_MISSING)
		return
	}

	data["userid"] = userId
	code := account.GetAccountService().UpdateUserInfo(data)
	p.Resp["code"] = code
	p.Resp["msg"] = errorcode.Msg(code)
}

// @router /userprofile/del [post]
func (p *UserInfoController) DeleteUser() {
	defer p.Output("DeleteUser")
	body := p.Ctx.Input.RequestBody
	data := make(map[string]interface{})
	if err := json.Unmarshal(body, &data); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	userid := p.GetString("uid", "")
	if userid == "" {
		p.Resp["code"] = errorcode.EC_USER_INFO_USER_ID_MISSING
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_USER_INFO_USER_ID_MISSING)
		return
	}

	code := account.GetAccountService().DeleteUser(userid)
	p.Resp["code"] = code
	p.Resp["msg"] = errorcode.Msg(code)
}
